#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
struct node{
	int x,y;
}a[N];
int n,ans,sum,A,B,siz[N],tlh[N];
bool cmp1(node &a,node &b){
	return a.x<b.x;
}
bool cmp2(node &a,node &b){
	return a.y<b.y;
}
map<pair<int,int>,int> mp;
vector<int> e[N];
void dfs1(int x,int f,int d){
	tlh[1]+=d;siz[x]=1;
	for(auto v:e[x]){
		if(v==f) continue;
		dfs1(v,x,d+1);
		siz[x]+=siz[v];
	}
}
void dfs(int x,int f){
	for(auto v:e[x]){
		if(v==f) continue;
		tlh[v]=tlh[x]-siz[v]+n-siz[v];dfs(v,x);
	}
}
queue<pair<int,int> > q;
int vis[4005];
void bfs(int g){
	q.push({g,0});
	while(!q.empty()){
		int x=q.front().first,bs=q.front().second;q.pop();
		if(vis[x]) continue;
		if(x==g) vis[x]=-1;
		else vis[x]=bs;
		for(auto v:e[x])
			if(!vis[v]) q.push({v,bs+1});
	}
}
signed main() {
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,mp[{a[i].x,a[i].y}]=i;
	for(int i=1;i<=n;i++){
		int id=mp[{a[i].x-1,a[i].y}];
		if(id) e[i].push_back(id);
		id=mp[{a[i].x+1,a[i].y}];
		if(id) e[i].push_back(id);
		id=mp[{a[i].x,a[i].y-1}];
		if(id) e[i].push_back(id);
		id=mp[{a[i].x,a[i].y+1}];
		if(id) e[i].push_back(id);
	}
	if(n<=4000){
		for(int i=1;i<=n;i++){
			memset(vis,0,sizeof(vis));
			bfs(i);
			for(int j=1;j<=n;j++) if(j!=i) ans+=vis[j];
		}
		cout<<ans/2,exit(0);
	}
	sort(a+1,a+n+1,cmp1);A=a[n].x-a[1].x+1;
	for(int i=n;i>=1;i--){
		ans+=sum-(n-i)*a[i].x;
		sum+=a[i].x;
	}
	sort(a+1,a+n+1,cmp2);sum=0;B=a[n].y-a[1].y+1;
	for(int i=n;i>=1;i--){
		ans+=sum-(n-i)*a[i].y;
		sum+=a[i].y;
	}
	if(A*B==n) cout<<ans,exit(0);
	ans=0;
	dfs1(1,0,0);
	dfs(1,0);
	for(int i=1;i<=n;i++) ans+=tlh[i];
	cout<<ans/2;
	return 0;
}
